#!/bin/bash
#
# xenballoond	Script to start and stop Xen ballooning daemon.
#
# Copyright (C) 2009 Oracle Corporation and/or its affiliates.
# All rights reserved.
# Written by: Dan Magenheimer <dan.magenheimer@oracle.com>
#
# chkconfig: 2345 98 01
# description: Starts and stops the Xen ballooning daemon.
### BEGIN INIT INFO
# Provides:          xenballoond
# Required-Start:    $syslog $remote_fs
# Should-Start:
# Required-Stop:     $syslog $remote_fs
# Should-Stop:
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Start/stop xenballoond
# Description:       Starts and stops the Xen ballooning daemon.
### END INIT INFO

# Source function library
. /etc/init.d/functions

#don't use in domain0
[ -f /proc/xen/capabilities ] && \
	grep -q "control_d" /proc/xen/capabilities && exit 0

if [ -f /etc/sysconfig/xenballoon.conf ]; then
	. /etc/sysconfig/xenballoon.conf
elif [ -f /etc/default/xenballoon.conf ]; then
	. /etc/default/xenballoon.conf
fi

# Check that balloon driver is present
[ ! -f /proc/xen/balloon ] && exit 0

# Record original memory (in kB)
[ -z "$XENBALLOON_MAXMEMFILE" ] && exit 0
let maxmem=`grep MemTotal /proc/meminfo | sed 's/  */ /' | cut -f2 -d' '`
if [ -f "$XENBALLOON_MAXMEMFILE" ]; then
	let oldmax=`cat $XENBALLOON_MAXMEMFILE`
	if [ $oldmax -gt $maxmem ]; then
		let maxmem=oldmax
	fi
fi
echo $maxmem > $XENBALLOON_MAXMEMFILE

RETVAL=0
prog="xenballoond"

start() {
        # Start daemons.
        echo -n $"Starting $prog: "
        daemon xenballoond $OPTIONS
	RETVAL=$?
        echo
	return $RETVAL
}

stop() {
        echo -n $"Shutting down $prog: "
	killproc xenballoond
	RETVAL=$?
        echo
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
        ;;
  stop)
	stop
        ;;
  status)
	status xenballoond
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit $RETVAL
